// JavaScript Document

Event.observe(window,'load',initLang);
Event.observe(window,'load',changeLang);
//Event.observe(window,'load',function(){alert('aa');});
//window.onload=initLang;

function initLang(){
	var lang = navigator.browserLanguage ? navigator.browserLanguage : navigator.language;
	if(lang.indexOf('zh')){
		var langFile='zh-cn';
	}else{
		var langFile='en';	
	}
	var url='i18n/'+langFile+'.json';
	new Ajax.Request(url,{
		onComplete:function(resp){
			var json=resp.responseText.evalJSON();
			//alert(json.lang.title);
			i18nCore(json);
			changeLang();
		}			 
	});
}

function changeLang(){
	$('zh-cn').onclick=function(){
		var url='i18n/'+this.id+'.json';
		new Ajax.Request(url,{
			onComplete:function(resp){
				var json=resp.responseText.evalJSON();
				i18nCore(json);
			}
		});
	}
	$('en').onclick=$('zh-cn').onclick;
}

function i18nCore(json){
	var url="temp/test.html";
	new Ajax.Request(url,{
		onComplete:function(resp){
			var str=resp.responseText;
			while(/#.+#/g.test(str)){
				index=/#.+#/.exec(str);
				temp=index;
				var key=index.toString().replace(/#/g,'');
				var lg=json.lang[key];
				//alert(typeof key);
				str=str.replace(temp,lg);
				$('main').innerHTML=str;
			}
		}			 
	});
}